﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace YJ.Data.MySql
{
    public class DocumentsReadUsers : YJ.Data.Interface.IDocumentsReadUsers
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public DocumentsReadUsers()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.DocumentsReadUsers实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.DocumentsReadUsers model)
        {
            string sql = @"INSERT INTO documentsreadusers
				(DocumentID,UserID,IsRead) 
				VALUES(@DocumentID,@UserID,@IsRead)";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@DocumentID", MySqlDbType.VarChar, 36){ Value = model.DocumentID },
				new MySqlParameter("@UserID", MySqlDbType.VarChar, 36){ Value = model.UserID },
				new MySqlParameter("@IsRead", MySqlDbType.Int32, 11){ Value = model.IsRead }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.DocumentsReadUsers实体类</param>
        public int Update(YJ.Data.Model.DocumentsReadUsers model)
        {
            string sql = @"UPDATE documentsreadusers SET 
				IsRead=@IsRead
				WHERE DocumentID=@DocumentID and UserID=@UserID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@IsRead", MySqlDbType.Int32, 11){ Value = model.IsRead },
				new MySqlParameter("@DocumentID", MySqlDbType.VarChar, 36){ Value = model.DocumentID },
				new MySqlParameter("@UserID", MySqlDbType.VarChar, 36){ Value = model.UserID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid documentid, Guid userid)
        {
            string sql = "DELETE FROM documentsreadusers WHERE DocumentID=@DocumentID AND UserID=@UserID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@DocumentID", MySqlDbType.VarChar, 36){ Value = documentid.ToString() },
				new MySqlParameter("@UserID", MySqlDbType.VarChar, 36){ Value = userid.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.DocumentsReadUsers> DataReaderToList(MySqlDataReader dataReader)
        {
            List<YJ.Data.Model.DocumentsReadUsers> List = new List<YJ.Data.Model.DocumentsReadUsers>();
            YJ.Data.Model.DocumentsReadUsers model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.DocumentsReadUsers();
                model.DocumentID = dataReader.GetString(0).ToGuid();
                model.UserID = dataReader.GetString(1).ToGuid();
                model.IsRead = dataReader.GetInt32(2);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.DocumentsReadUsers> GetAll()
        {
            string sql = "SELECT * FROM documentsreadusers";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.DocumentsReadUsers> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM documentsreadusers";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.DocumentsReadUsers Get(Guid documentid, Guid userid)
        {
            string sql = "SELECT * FROM documentsreadusers WHERE DocumentID=@DocumentID AND UserID=@UserID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@DocumentID", MySqlDbType.VarChar, 36){ Value = documentid.ToString() },
				new MySqlParameter("@UserID", MySqlDbType.VarChar, 36){ Value = userid.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.DocumentsReadUsers> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid documentid)
        {
            string sql = "DELETE FROM DocumentsReadUsers WHERE DocumentID=@DocumentID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@DocumentID", MySqlDbType.VarChar){ Value = documentid }
			};
            return dbHelper.Execute(sql, parameters);
        }

        /// <summary>
        /// 更新一个文档为已读
        /// </summary>
        /// <param name="docID"></param>
        /// <param name="userID"></param>
        public void UpdateRead(Guid docID, Guid userID)
        {
            string sql = "UPDATE DocumentsReadUsers SET IsRead=1 WHERE DocumentID=@DocumentID AND UserID=@UserID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@DocumentID", MySqlDbType.VarChar){ Value = docID },
				new MySqlParameter("@UserID",MySqlDbType.VarChar){ Value = userID }
			};
            dbHelper.Execute(sql, parameters);
        }
    }
}